//
//  MakeWishViewController.m
//  一点互助
//
//  Created by rimi on 16/8/25.
//  Copyright © 2016年 zhangboqiu. All rights reserved.
//

#import "MakeWishViewController.h"
#import "ResponseManager.h"
#import "UIViewController+NavagitionBar.h"
#import "MakeWishDetailViewController.h"

@interface MakeWishViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *wishTitle;
@property (weak, nonatomic) IBOutlet UITextView *wishDescTextview;
@property (weak, nonatomic) IBOutlet UIImageView *image1;
@property (weak, nonatomic) IBOutlet UIImageView *image2;
@property (weak, nonatomic) IBOutlet UIImageView *image3;
@property (strong, nonatomic) UIBarButtonItem *btn;
@end

@implementation MakeWishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addNextBtn];
    
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.tabBarController.tabBar setHidden:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self.tabBarController.tabBar setHidden:NO];
}
-(void)addNextBtn{
    [self.navigationItem setRightBarButtonItem:self.btn];

}
- (IBAction)getPicture:(id)sender {
    __weak typeof(self) myself = self;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        PLLOG(@"1");
        UIImagePickerController *picekerVc = [[UIImagePickerController alloc] init];
        picekerVc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        picekerVc.delegate = self;
        [self presentViewController:picekerVc animated:YES completion:nil];

    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        PLLOG(@"2");
        [self invokeCamera];
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:action1];
    [alert addAction:action2];
    [alert addAction:action3];
    [self presentViewController:alert animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    NSLog(@"%@", info);// 打印出相应的key信息
    UIImage *image = [info objectForKey: UIImagePickerControllerOriginalImage];
    //    UIImagePickerControllerEditedImage  拍完照后可以进行编辑

    [picker dismissViewControllerAnimated:YES completion:^{
        if (self.image1.image == nil && self.image2.image == nil && self.image3.image == nil) {
            self.image1.image = image;
        }else if(self.image2.image == nil && self.image3.image == nil && self.image1.image != nil){
            self.image2.image = image;
        }else if(self.image1.image != nil && self.image2.image != nil && self.image3.image == nil){
            self.image3.image = image;
        }else{
            [self addAlertControllerWithTitle:@"提示" message:@"最多上传3张图片" actionNum:1 style:UIAlertControllerStyleAlert handler:nil];
        }
    }];

}
-(UIBarButtonItem *)btn{
    if (!_btn) {
        _btn = [[UIBarButtonItem alloc]initWithTitle:@"下一步" style:UIBarButtonItemStylePlain target:self action:@selector(getNext)];
    }
    return _btn;
}
-(void)getNext{
    PLLOG(@"next");
    if (self.wishTitle.text.length == 0) {
        [self addAlertControllerWithTitle:@"提示" message:@"请输入愿望标题" actionNum:1 style:UIAlertControllerStyleAlert handler:nil];
    }else if (self.wishDescTextview.text.length == 0){
        [self addAlertControllerWithTitle:@"提示" message:@"请输入愿望内容" actionNum:1 style:UIAlertControllerStyleAlert handler:nil];
    }else{
        MakeWishDetailViewController *vc = [[MakeWishDetailViewController alloc]init];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        dic[@"wish_title"] = self.wishTitle.text;
        dic[@"wish_desc"] = self.wishDescTextview.text;
        if (self.image1.image == nil && self.image2.image == nil && self.image3.image == nil) {
            dic[@"pic"] = [NSArray array];
            vc.dic = dic;
            [self.navigationController pushViewController:vc animated:YES];
            PLLOG(@"%@",dic);
        }else if (self.image1.image != nil && self.image2.image == nil){
            dic[@"pic"] = [NSArray arrayWithObjects:self.image1.image, nil];
            vc.dic = dic;
            [self.navigationController pushViewController:vc animated:YES];
            PLLOG(@"%@",dic);
        }else if (self.image1.image != nil && self.image2.image != nil && self.image3.image == nil){
            dic[@"pic"] = [NSArray arrayWithObjects:self.image1.image,self.image2.image, nil];
            vc.dic = dic;
            PLLOG(@"%@",dic);
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            dic[@"pic"] = [NSArray arrayWithObjects:self.image1.image,self.image2.image,self.image3.image, nil];
            vc.dic = dic;
            PLLOG(@"%@",dic);
            [self.navigationController pushViewController:vc animated:YES];
        }

    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -- 调用摄像头

// 调用摄像头
- (void)invokeCamera{
    UIImagePickerController *imagePick = [[UIImagePickerController alloc] init];
    BOOL iscamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
    if (!iscamera) {
        [self addAlertControllerWithTitle:@"温馨提示" message:@"木有色相头" actionNum:1 style:UIAlertControllerStyleAlert handler:nil];
        NSLog(@"没有摄像头");
        return ;
    }
    
    // 判断是否有后置摄像头
    //        UIImagePickerControllerCameraDeviceFront ,为前置摄像头
    
    imagePick.delegate = self;
    imagePick.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePick.allowsEditing = YES; //拍完照可以进行编辑
    [self presentViewController:imagePick animated:YES completion:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
